package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmProduct;
import com.ruoyi.crm.service.ICrmProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品列表Controller
 *
 * @author yanwenliang
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/crm/product")
public class CrmProductController extends BaseController
{
    @Autowired
    private ICrmProductService crmProductService;

    /**
     * 查询产品列表列表
     */
    @PreAuthorize("@ss.hasPermi('crm:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmProduct crmProduct)
    {
        startPage();
        List<CrmProduct> list = crmProductService.selectCrmProductList(crmProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表列表
     */
    @PreAuthorize("@ss.hasPermi('crm:product:export')")
    @Log(title = "产品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmProduct crmProduct)
    {
        List<CrmProduct> list = crmProductService.selectCrmProductList(crmProduct);
        ExcelUtil<CrmProduct> util = new ExcelUtil<CrmProduct>(CrmProduct.class);
        util.exportExcel(response, list, "产品列表数据");
    }

    /**
     * 获取产品列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") String productId)
    {
        return success(crmProductService.selectCrmProductByProductId(productId));
    }

    /**
     * 新增产品列表
     */
    @PreAuthorize("@ss.hasPermi('crm:product:add')")
    @Log(title = "产品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmProduct crmProduct)
    {
        return toAjax(crmProductService.insertCrmProduct(crmProduct));
    }

    /**
     * 修改产品列表
     */
    @PreAuthorize("@ss.hasPermi('crm:product:edit')")
    @Log(title = "产品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmProduct crmProduct)
    {
        return toAjax(crmProductService.updateCrmProduct(crmProduct));
    }

    /**
     * 删除产品列表
     */
    @PreAuthorize("@ss.hasPermi('crm:product:remove')")
    @Log(title = "产品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable String[] productIds)
    {
        return toAjax(crmProductService.deleteCrmProductByProductIds(productIds));
    }

    /**
     * 获取产品列表
     * @param crmProduct
     * @return
     */
    @PostMapping("getProductListNoPage")
    public AjaxResult getProductListNoPage(@RequestBody CrmProduct crmProduct){
        return AjaxResult.success(crmProductService.list(new LambdaQueryWrapper<CrmProduct>().orderByDesc(CrmProduct::getCreateTime)));
    }
}
